package com.jmxcfc.blfsc.common.common.utils;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.jmxcfc.blfsc.common.sign.SignConstants;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDateTime;

/**
 * 功能描述
 * 文件工具类
 * @author: pbs
 * @date: 2024年08月29日 19:02
 */
public class FileUtils {
    private FileUtils() throws IllegalAccessException {
        throw new IllegalAccessException("非法调用!");
    }

    /**
     * 创建文件夹
     * @param path
     * @throws IOException
     */
    public static void createDirectories(Path path) throws IOException {
        if (Files.notExists(path)) {
            Files.createDirectories(path);
        }
    }

    public static String getFileNameByPath(String filePath, boolean subFileType) {
        String fileName = Paths.get(filePath).getFileName().toString();
        if (subFileType) {
            fileName = StringUtils.substringBeforeLast(fileName, ".");
        }
        return fileName;
    }


    /**
     * 创建地址
     * @param rootPath
     * @param prodCode
     * @param templePdfPath
     * @return
     * @throws Exception
     */
    public static  String createOutputFilepath(String rootPath,String prodCode,String templePdfPath) throws Exception {
        String datePath = LocalDateTimeUtil.format(LocalDateTime.now(), SignConstants.PATH_DATE_FORMATTER);
        Path localOutputFilepath = Paths.get(rootPath, datePath, prodCode);
        FileUtils.createDirectories(localOutputFilepath);
        String templePdfFileName = FileUtils.getFileNameByPath(templePdfPath, false);
        return StringUtils.replace(Paths.get(localOutputFilepath.toString(), templePdfFileName).toString(), "\\", "/");
    }

}